﻿using System;
using Microsoft.EntityFrameworkCore;

namespace LanhuFx.Context.PgSql.ResourcesModels
{
    public partial class LanhuFxContext : DbContext
    {
        public LanhuFxContext()
        {
        }

        public LanhuFxContext(DbContextOptions<LanhuFxContext> options)
            : base(options)
        {
        }

        public readonly Guid? Teamid;

        public LanhuFxContext(DbContextOptions<LanhuFxContext> options, Guid? arg_Teamid) : base(options)
        {
            Teamid = arg_Teamid;
        }

        public virtual DbSet<IdentityAccount> IdentityAccount { get; set; }
        public virtual DbSet<IdentityLoginlog> IdentityLoginlog { get; set; }
        public virtual DbSet<IdentityPermissionsetting> IdentityPermissionsetting { get; set; }
        public virtual DbSet<IdentityRole> IdentityRole { get; set; }
        public virtual DbSet<IdentityRolepermissions> IdentityRolepermissions { get; set; }
        public virtual DbSet<IdentitySyapiserviceConfig> IdentitySyapiserviceConfig { get; set; }
        public virtual DbSet<IdentityTenant> IdentityTenant { get; set; }
        public virtual DbSet<IdentityUserinfo> IdentityUserinfo { get; set; }
        public virtual DbSet<IdentityUserpermissions> IdentityUserpermissions { get; set; }
        public virtual DbSet<IdentityUserRole> IdentityUserrole { get; set; }
        public virtual DbSet<IdentityUsertenant> IdentityUsertenant { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasPostgresExtension("uuid-ossp");

            modelBuilder.Entity<IdentityAccount>(entity =>
            {
                entity.ToTable("identity_account");

                entity.HasQueryFilter(n => n.Teamid == Teamid);

                entity.Property(e => e.Id)
                                    .HasColumnName("id")
                                    .ValueGeneratedNever();

                entity.Property(e => e.Account)
                    .HasColumnName("account")
                    .HasMaxLength(255)
                    .IsFixedLength();

                entity.Property(e => e.Accounttype)
                    .HasColumnName("accounttype")
                    .HasMaxLength(1000)
                    .IsFixedLength();

                entity.Property(e => e.Createdat).HasColumnName("createdat");

                entity.Property(e => e.Password)
                    .HasColumnName("password")
                    .HasMaxLength(1000)
                    .IsFixedLength();

                entity.Property(e => e.Teamid).HasColumnName("teamid");

                entity.Property(e => e.Tokenid)
                    .HasColumnName("tokenid")
                    .HasMaxLength(1000)
                    .IsFixedLength();

                entity.Property(e => e.Userid).HasColumnName("userid");

                entity.HasOne(d => d.Team)
                    .WithMany(p => p.IdentityAccount)
                    .HasForeignKey(d => d.Teamid)
                    .HasConstraintName("identity_account_teamid_fkey");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.IdentityAccount)
                    .HasForeignKey(d => d.Userid)
                    .HasConstraintName("identity_account_userid_fkey");
            });

            modelBuilder.Entity<IdentityLoginlog>(entity =>
            {
                entity.ToTable("identity_loginlog");

                entity.HasQueryFilter(n => n.Teamid == Teamid);
                entity.Property(e => e.Id)
                                    .HasColumnName("id")
                                    .ValueGeneratedNever();

                entity.Property(e => e.Accountid).HasColumnName("accountid");

                entity.Property(e => e.Createdat).HasColumnName("createdat");

                entity.Property(e => e.Deviceinfo)
                    .HasColumnName("deviceinfo")
                    .HasMaxLength(1000)
                    .IsFixedLength();

                entity.Property(e => e.Teamid).HasColumnName("teamid");

                entity.Property(e => e.Terminalid).HasColumnName("terminalid");

                entity.Property(e => e.Userid).HasColumnName("userid");

                entity.HasOne(d => d.Team)
                    .WithMany(p => p.IdentityLoginlog)
                    .HasForeignKey(d => d.Teamid)
                    .HasConstraintName("identity_loginlog_teamid_fkey");
            });

            modelBuilder.Entity<IdentityPermissionsetting>(entity =>
            {
                entity.ToTable("identity_permissionsetting");

                entity.HasQueryFilter(n => n.Teamid == Teamid);
                entity.Property(e => e.Id)
                                    .HasColumnName("id")
                                    .ValueGeneratedNever();

                entity.Property(e => e.Groupname)
                    .HasColumnName("groupname")
                    .HasMaxLength(255)
                    .IsFixedLength();

                entity.Property(e => e.Isroot)
                    .HasColumnName("isroot")
                    .HasColumnType("bit(1)");

                entity.Property(e => e.Modular)
                    .HasColumnName("modular")
                    .HasMaxLength(255)
                    .IsFixedLength();

                entity.Property(e => e.Name)
                    .HasColumnName("name")
                    .HasMaxLength(255)
                    .IsFixedLength();

                entity.Property(e => e.Parentid).HasColumnName("parentid");

                entity.Property(e => e.Permissiontype).HasColumnName("permissiontype");

                entity.Property(e => e.Sortvalue).HasColumnName("sortvalue");

                entity.Property(e => e.Teamid).HasColumnName("teamid");

                entity.HasOne(d => d.Parent)
                    .WithMany(p => p.InverseParent)
                    .HasForeignKey(d => d.Parentid)
                    .HasConstraintName("identity_permissionsetting_parentid_fkey");

                entity.HasOne(d => d.Team)
                    .WithMany(p => p.IdentityPermissionsetting)
                    .HasForeignKey(d => d.Teamid)
                    .HasConstraintName("identity_permissionsetting_teamid_fkey");
            });

            modelBuilder.Entity<IdentityRole>(entity =>
            {
                entity.ToTable("identity_role");

                entity.HasQueryFilter(n => n.Teamid == Teamid);
                entity.Property(e => e.Id)
                                    .HasColumnName("id")
                                    .ValueGeneratedNever();

                entity.Property(e => e.Issystem)
                    .HasColumnName("issystem")
                    .HasColumnType("bit(1)");

                entity.Property(e => e.Name)
                    .HasColumnName("name")
                    .HasMaxLength(50)
                    .IsFixedLength();

                entity.Property(e => e.Systemcode)
                    .HasColumnName("systemcode")
                    .HasMaxLength(50)
                    .IsFixedLength();

                entity.Property(e => e.Teamid).HasColumnName("teamid");

                entity.HasOne(d => d.Team)
                    .WithMany(p => p.IdentityRole)
                    .HasForeignKey(d => d.Teamid)
                    .HasConstraintName("identity_role_teamid_fkey");
            });

            modelBuilder.Entity<IdentityRolepermissions>(entity =>
            {
                entity.ToTable("identity_rolepermissions");

                entity.HasQueryFilter(n => n.Teamid == Teamid);
                entity.Property(e => e.Id)
                                    .HasColumnName("id")
                                    .ValueGeneratedNever();

                entity.Property(e => e.Createat).HasColumnName("createat");

                entity.Property(e => e.Permissiontype).HasColumnName("permissiontype");

                entity.Property(e => e.Promissionid).HasColumnName("promissionid");

                entity.Property(e => e.Roleid).HasColumnName("roleid");

                entity.Property(e => e.Teamid).HasColumnName("teamid");

                entity.HasOne(d => d.Promission)
                    .WithMany(p => p.IdentityRolepermissions)
                    .HasForeignKey(d => d.Promissionid)
                    .HasConstraintName("identity_rolepermissions_promissionid_fkey");

                entity.HasOne(d => d.Role)
                    .WithMany(p => p.IdentityRolepermissions)
                    .HasForeignKey(d => d.Roleid)
                    .HasConstraintName("identity_rolepermissions_roleid_fkey");

                entity.HasOne(d => d.Team)
                    .WithMany(p => p.IdentityRolepermissions)
                    .HasForeignKey(d => d.Teamid)
                    .HasConstraintName("identity_rolepermissions_teamid_fkey");
            });

            modelBuilder.Entity<IdentitySyapiserviceConfig>(entity =>
            {
                entity.HasKey(e => e.Teamid)
                    .HasName("identity_syapiservice_config_pkey");

                entity.ToTable("identity_syapiservice_config");

                entity.HasQueryFilter(n => n.Teamid == Teamid);

                entity.Property(e => e.Teamid)
                    .HasColumnName("teamid")
                    .ValueGeneratedNever();

                entity.Property(e => e.SyApiurl)
                    .HasColumnName("sy_apiurl")
                    .HasMaxLength(255)
                    .IsFixedLength();

                entity.Property(e => e.SyAppcode)
                    .HasColumnName("sy_appcode")
                    .HasMaxLength(255)
                    .IsFixedLength();

                entity.HasOne(d => d.Team)
                    .WithOne(p => p.IdentitySyapiserviceConfig)
                    .HasForeignKey<IdentitySyapiserviceConfig>(d => d.Teamid)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("identity_syapiservice_config_teamid_fkey");
            });

            modelBuilder.Entity<IdentityTenant>(entity =>
            {
                entity.HasKey(e => e.Teamid)
                    .HasName("identity_tenant_pkey");

                entity.ToTable("identity_tenant");

                entity.HasQueryFilter(n => n.Teamid == Teamid);

                entity.Property(e => e.Teamid)
                    .HasColumnName("teamid")
                    .ValueGeneratedNever();

                entity.Property(e => e.Createdat).HasColumnName("createdat");

                entity.Property(e => e.Tenantcode)
                    .HasColumnName("tenantcode")
                    .HasMaxLength(50)
                    .IsFixedLength();

                entity.Property(e => e.Tenantname)
                    .HasColumnName("tenantname")
                    .HasMaxLength(1000)
                    .IsFixedLength();

                entity.Property(e => e.Tenantstate)
                    .HasColumnName("tenantstate")
                    .HasMaxLength(1000)
                    .IsFixedLength();

                entity.Property(e => e.Tenanttype)
                    .HasColumnName("tenanttype")
                    .HasMaxLength(1000)
                    .IsFixedLength();
            });

            modelBuilder.Entity<IdentityUserinfo>(entity =>
            {
                entity.ToTable("identity_userinfo");

                entity.HasQueryFilter(n => n.Teamid == Teamid);
                entity.Property(e => e.Id)
                                    .HasColumnName("id")
                                    .ValueGeneratedNever();

                entity.Property(e => e.Createdat).HasColumnName("createdat");

                entity.Property(e => e.Sysidentity)
                    .HasColumnName("sysidentity")
                    .HasMaxLength(1000)
                    .IsFixedLength();

                entity.Property(e => e.Teamid).HasColumnName("teamid");

                entity.Property(e => e.Username)
                    .HasColumnName("username")
                    .HasMaxLength(1000)
                    .IsFixedLength();

                entity.Property(e => e.Userstate)
                    .HasColumnName("userstate")
                    .HasMaxLength(1000)
                    .IsFixedLength();

                entity.HasOne(d => d.Team)
                    .WithMany(p => p.IdentityUserinfo)
                    .HasForeignKey(d => d.Teamid)
                    .HasConstraintName("identity_userinfo_teamid_fkey");
            });

            modelBuilder.Entity<IdentityUserpermissions>(entity =>
            {
                entity.ToTable("identity_userpermissions");

                entity.HasQueryFilter(n => n.Teamid == Teamid);
                entity.Property(e => e.Id)
                                    .HasColumnName("id")
                                    .ValueGeneratedNever();

                entity.Property(e => e.Createat).HasColumnName("createat");

                entity.Property(e => e.Permissiontype).HasColumnName("permissiontype");

                entity.Property(e => e.Promissionid).HasColumnName("promissionid");

                entity.Property(e => e.Teamid).HasColumnName("teamid");

                entity.Property(e => e.Userid).HasColumnName("userid");

                entity.HasOne(d => d.Promission)
                    .WithMany(p => p.IdentityUserpermissions)
                    .HasForeignKey(d => d.Promissionid)
                    .HasConstraintName("identity_userpermissions_promissionid_fkey");

                entity.HasOne(d => d.Team)
                    .WithMany(p => p.IdentityUserpermissions)
                    .HasForeignKey(d => d.Teamid)
                    .HasConstraintName("identity_userpermissions_teamid_fkey");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.IdentityUserpermissions)
                    .HasForeignKey(d => d.Userid)
                    .HasConstraintName("identity_userpermissions_userid_fkey");
            });

            modelBuilder.Entity<IdentityUserRole>(entity =>
            {
                entity.HasKey(e => new { e.Roleid, e.Userid })
                    .HasName("identity_userrole_pkey");

                entity.ToTable("identity_userrole");

                entity.HasQueryFilter(n => n.Teamid == Teamid);

                entity.Property(e => e.Roleid).HasColumnName("roleid");

                entity.Property(e => e.Userid).HasColumnName("userid");

                entity.Property(e => e.Teamid).HasColumnName("teamid");

                entity.HasOne(d => d.Role)
                    .WithMany(p => p.IdentityUserrole)
                    .HasForeignKey(d => d.Roleid)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("identity_userrole_roleid_fkey");

                entity.HasOne(d => d.Team)
                    .WithMany(p => p.IdentityUserrole)
                    .HasForeignKey(d => d.Teamid)
                    .HasConstraintName("identity_userrole_teamid_fkey");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.IdentityUserrole)
                    .HasForeignKey(d => d.Userid)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("identity_userrole_userid_fkey");
            });

            modelBuilder.Entity<IdentityUsertenant>(entity =>
            {
                entity.HasKey(e => new { e.Teamid, e.Userid })
                    .HasName("identity_usertenant_pkey");

                entity.ToTable("identity_usertenant");

                entity.HasQueryFilter(n => n.Teamid == Teamid);

                entity.Property(e => e.Teamid).HasColumnName("teamid");

                entity.Property(e => e.Userid).HasColumnName("userid");

                entity.HasOne(d => d.Team)
                    .WithMany(p => p.IdentityUsertenant)
                    .HasForeignKey(d => d.Teamid)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("identity_usertenant_teamid_fkey");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.IdentityUsertenant)
                    .HasForeignKey(d => d.Userid)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("identity_usertenant_userid_fkey");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
